PCX format tutorial
Back to index


A 256 color PCX file consist of a 128 byter long header, RLE encoded data and 768 byte of palette.
If you know the size of the image you probably don't need the header.

The header

Position Description Length in bytes Explanation
0 Manufacturer 1 byte
1 Version 1 byte
2 Encoding 1 byte 1 = RLE
3 Bits per pixel 1 byte 8 = 256 colors
4 Left x 2 bytes
6 Top y 2 bytes
8 Right x 2 bytes
10 Bottom y 2 bytes
12 Horizontal resolution in DPI 2 bytes
14 Vertical resolution in DPI 2 bytes
16 16 color palette 48 bytes Only if 16 color PCX
64 reserved 1 byte
65 Number of color planes 1 byte
66 Bytes per scanline 2 bytes
68 Palette information 2 bytes 1 = color, 2 = grayscale
70 Horizontal screen size 2 bytes
72 Vertican screen size 2 bytes
74 Nothing 54 byte Zeros

The data

RLE or run length encoding means that if a color is repeated the number of times and what color it was is stored rather than storing each pixel.
In this case it means that if the first byte is greater than 191 the next byte will be the color that should be repeated. To find out how many times it should be repeated subtract 192 from the first byte. If the first byte is below 192 it can be drawn directly.
The pixels start in the upper left corner and ends in the lower left corner.
This code decodes a pcx

    lea si,buffer+128
    push 0a000h
    pop es
    xor bx,bx
    decodea:
    cmp bx,dx
    je decodec
    lodsb
    cmp al,192
    ja decodeb
    stosb
    inc bx
    jmp decodea
    decodeb:
    movzx cx,al
    sub cl,192
    lodsb
    rep stosb
    inc bx
    inc bx
    jmp decodea
    decodec:

The palette

Each color takes up 3 bytes. One for red, one for green and one for blue.
Each value is 8 bit

Here is a sample program
pcx.zip (5019 bytes)


Jesper L. Poulsen